package com.easy.easy.Service.impl;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.easy.easy.Service.IOrderService;
import com.easy.easy.bean.Order;
import com.easy.easy.dao.OrderDao;
import com.easy.easy.util.Page;
import com.easy.easy.util.PaymentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.image.BufferedImage;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Service
public class OrderServiceimpl implements IOrderService {

    @Autowired
    OrderDao orderDao;

    @Override
    @Transactional
    public int add(Order order) {
        //设置订单创建时间
        if(order.getAddtime()==null){
            order.setAddtime(LocalDateTime.now());
        }
        //设置订单更新时间
        order.setUpdatetime(LocalDateTime.now());
       return orderDao.add(order);

    }

    @Override
    public int edit(Order order) {
        //设置订单更新时间
        order.setUpdatetime(LocalDateTime.now());
        return orderDao.edit(order);
    }

    @Override
    public int delete(int orderid, int userid) {
        return orderDao.delete(orderid, userid);
    }

    @Override
    public List<Order> getList(Page page, String text, int userid) {
        return orderDao.getList(page, text, userid);
    }

    @Override
    public List<Order> getlist(Page page, String text) {
        return orderDao.getlist(page, text);
    }

    @Override
    public List<Order> getOrderByUserId(int userid) {
        return orderDao.getOrderByUserId(userid);
    }

    @Override
    public Order getOrderById(int orderid, int userid) {
        return orderDao.getOrderById(orderid, userid);
    }

    @Override
    public int getCount(String text, int userid) {
        return orderDao.getCount(text, userid);
    }

    @Override
    public Order getOrder(Order order) {
        return orderDao.getOrder(order);
    }

    @Override
    public int cancleOrder() {
        return orderDao.cancleOrder();
    }

    @Override
    public Order getOrderByID(int orderid) {
        return orderDao.getOrderByID(orderid);
    }

    @Override
    public int Delete(int orderid) {
        return orderDao.Delete(orderid);
    }

    @Override
    public int paymentSuccess(HashMap map){
        //检查订单是否存在,是否是阿里后台的回调
        String code=(String)map.get("out_trade_no");
        System.out.println(code);


        return orderDao.paymentSuccess(code);
    }

    @Override
    public int getcount(String text) {
        return orderDao.getcount(text);
    }

    @Override
    public int delivery(int orderid) {
        return orderDao.delivery(orderid);
    }

    @Override
    public int getproduct(int orderid) {
        return orderDao.getproduct(orderid);
    }


    @Override
    public String payment(int orderid){
        Order order=orderDao.getOrderByID(orderid);
        if(order==null){
            throw new RuntimeException("订单不存在");
        }
        if(order.getState()==-1){
            throw new RuntimeException("订单已失效");
        }
        if(order.getState()!=0){
            throw new RuntimeException("订单已支付");
        }
        //准备支付--生成二维码
        String url=null;
        try {

            url= PaymentUtil.payment(order);
            System.out.println("url--------------------------"+url);
        }catch(Exception e){
            System.out.println("url="+url);
            throw new RuntimeException("支付服务异常");
        }
        //生成二维码图片
        BufferedImage img= QrCodeUtil.generate(url, 300, 300);
        //将图片转成Base64编码
        String qrcode= ImgUtil.toBase64(img,"png");
        System.out.println(qrcode);
        return qrcode;
    }
}
